/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.facilities.utils;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  工具类
 * 
 * @author user
 */
public class Format {

    
    public static String null2Blank(Object str) {
        String result = "";
        if(null == str || str.equals("null")) {
            return result;
        }
        return str.toString();
    }
    
    public static int str2Int(Object obj) {
        int intVal = -1;
        if(null == obj) {
            return intVal;
        }
        try {
            intVal = Integer.parseInt(obj.toString());
        } catch (Exception e) {
            intVal = -1;
        }
        return intVal;
    }
    
    /**
     * 中文字符串解码
     * @param str
     * @param charsetfrom
     * @param charsetto
     * @return 
     */
    public static String chartsetString(String str, String charsetfrom, String charsetto) {
        try {
            return new String(str.getBytes(charsetfrom), charsetto);
        } catch (UnsupportedEncodingException ex) {
            return str;
        }
    }
    
    /**
     * 中文字符串解码
     * @param str
     * @param charsetfrom
     * @param charsetto
     * @return 
     */
    public static String iso2utf8(String str) {
        try {
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            return str;
        }
    }
    
    /**
     * 格式化日期
     * @param date
     * @return 
     */
    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
}
